//
//  HotsViewController.m
//  QuicklyPlay
//
//  Created by 你的温柔是我唯一沉溺 on 15/10/31.
//  Copyright © 2015年 OPteam. All rights reserved.
//

#import "HotsViewController.h"
#import "HotsCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MJRefresh.h"
#import "HotsDetailViewController.h"

@interface HotsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NetworkHelper *netWorkHelper;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property (strong, nonatomic) HotsDetailViewController *hotsDetailVC;

@end

@implementation HotsViewController


-(NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(NetworkHelper *)netWorkHelper
{
    if (_netWorkHelper == nil) {
        _netWorkHelper = [NetworkHelper sharedInstance];
        
    }
    return _netWorkHelper;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    [self.hostTableView registerNib:[UINib nibWithNibName:@"HotsCell" bundle:nil] forCellReuseIdentifier:@"host"];
    //[self funNetwork];

    self.hostTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    [self.hostTableView.header beginRefreshing];
    [self funNetwork];
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}


- (void) funNetwork {


    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        

        if (status <= 0) {
        
        } else {
            NSLog(@"有网了(热门)");
            [self loadMore];
        }
    }];
}



-(void)loadMore
{

//    [self.netWorkHelper downloadWithURL:k_HotsURL WithRequestAllDataDidFinish:^{
//        [self.hostTableView reloadData];
//    }];
    
//    self.view.userInteractionEnabled = NO;
//    [self.netWorkHelper downloadWithURL:k_HotsURL WithRequestAllDataDidFinish:^(BOOL status) {
//
//        if (status) {
//            [self.hostTableView reloadData];
////            self.view.userInteractionEnabled = YES;
//            [self.hostTableView.header endRefreshing];
//        } else {
//            NSLog(@"没有网络(Hots)");
////            self.view.userInteractionEnabled = YES;
//            [self.hostTableView reloadData];
//
//            [self.hostTableView.header endRefreshing];
//        }
//    
//    }];
    
    [self.netWorkHelper downloadWithURL:k_HotsURL WithRequestAllDataDidFinish:^(BOOL status) {
        if (status) {
            [self.hostTableView reloadData];
            [self.hostTableView.header endRefreshing];
        } else {
            [self.hostTableView.header endRefreshing];
        }
    }];
    

//    self.hostTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //  [self.netWorkHelper.allVideosArray removeAllObjects];
//        self.view.userInteractionEnabled = NO;
//        [self.hostTableView.header beginRefreshing];
        
        //[self.hostTableView.header performSelector:@selector(endfresh) withObject:self afterDelay:8.0] ;
        
//        self.netWorkHelper.allVideosArray = [self.dataArray copy];
        
        //NSLog(@"%@",self.netWorkHelper.allVideosArray);
        
//        [self.netWorkHelper downloadWithURL:k_HotsURL WithRequestAllDataDidFinish:^{
//      
//            [self.hostTableView reloadData];
//            
//            self.view.userInteractionEnabled = YES;
//            
//            [self.hostTableView.header endRefreshing];
//        }];
//        
//        [self.netWorkHelper downloadWithURL:k_HotsURL WithRequestAllDataDidFinish:^(BOOL status) {
//            if (status) {
//                 [self.hostTableView reloadData];
//                 self.view.userInteractionEnabled = YES;
//                 [self.hostTableView.header endRefreshing];
//            } else {
//                
//                NSLog(@"没有网络(Hots)");
//                [self.hostTableView.header endRefreshing];
//
//            }
//        }];
//        
//        
//        
//    }];


}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.netWorkHelper.allVideosArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"%@", tableView);
    HotsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"host" forIndexPath:indexPath];
    
    VideosModel *model = self.netWorkHelper.allVideosArray[indexPath.row];
    
    self.index = indexPath.row;
    
    [cell cellSetMessageWithModel:model];
    
    NSString *string = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    
    if (indexPath.row < 3) {
        [cell.rankImage setImage:[UIImage imageNamed:@"rank123.png"]];
    } else {
        [cell.rankImage setImage:[UIImage imageNamed:@"rank456.png"]];
    }
    
    cell.getModel = model;
    cell.numberLabel.text = string;
    
    UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView = backView;
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.shareBtn addTarget:self action:@selector(shareBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    self.hotsDetailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HotsDetailVC"];
    
    self.hotsDetailVC.reModel = self.netWorkHelper.allVideosArray[indexPath.row];
    
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    [tempAppDelegate.window.rootViewController presentViewController:self.hotsDetailVC animated:YES completion:nil];
}


- (void) shareBtnAction:(UIButton *)sender {
    
    VideosModel *model = self.netWorkHelper.allVideosArray[self.index];
    
     [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5649a0eb67e58ebef3000074"shareText:model.share_url shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,nil] delegate:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}




@end
